package com.geekbang;

import com.geekbang.person.Customer;
import com.geekbang.supermarket.LittleSuperMarket;
import com.geekbang.supermarket.Merchandise;

import java.util.Scanner;

public class RunLittleSuperMarketAppMain {
    public static void main(String[] args) {
        LittleSuperMarket littleSuperMarket = new LittleSuperMarket();
        littleSuperMarket.address = "山阳大道";
        littleSuperMarket.superMarketname = "好又多超市";
        littleSuperMarket.parkingCount = 200;
        littleSuperMarket.merchandises = new Merchandise[200];//商品种类数量
        littleSuperMarket.merchandiseSold = new int[littleSuperMarket.merchandises.length];

        Merchandise[] all = littleSuperMarket.merchandises;//用all简化
        for (int i = 0; i < all.length; i++) {
            Merchandise m = new Merchandise();
            m.count = 200;
            m.name = "商品" + i;
            m.id = "ID" + i;
            m.purchasePrice = Math.random() * 200;
            m.soldPrice = ((1 + Math.random()) * 200);
            all[i] = m;
        }
        boolean open = true;
        Scanner scanner = new Scanner(System.in);
        while (open) {
            System.out.println("超市开门啦！");
            System.out.println("这里是" + littleSuperMarket.superMarketname);
            System.out.println("本店地址在" + littleSuperMarket.address);
            System.out.println("共有停车位" + littleSuperMarket.parkingCount + "个");
            System.out.println("本店的销售额为" + littleSuperMarket.incomingSum);
            System.out.println("共有商品" + littleSuperMarket.merchandises.length + "种");

            Customer customer = new Customer();
            customer.name = "顾客编号" + Math.random();
            customer.money = 2000;
            customer.isDrivingCar = Math.random() > 0.5;

            if (customer.isDrivingCar) {
                if (littleSuperMarket.parkingCount > 0) {
                    System.out.println("欢迎" + customer.name + "驾车而来，停车位编号为" + littleSuperMarket.parkingCount);
                    littleSuperMarket.parkingCount--;
                } else {
                    System.out.println("停车位已经满了，欢迎下次光临");
                    continue;
                }
            } else {
                System.out.println("欢迎" + customer.name + "光临");
            }

            double totalCost = 0;
            while (true) {
                System.out.println("本店提供" + all.length + "件商品，欢迎选购，请输入商品编号：");
                int index = scanner.nextInt();
                if (index < 0) {
                    break;
                }
                if (index >= all.length) {
                    System.out.println("本店没有这种商品，请输入1至" + (all.length - 1) + "的编号");
                    continue;
                }
                Merchandise m = all[index];
                System.out.println("您选购的商品名字是：" + m.name + ",商品单价是：" + m.soldPrice + "请问您要购买多少个？");
                int numToBuy = scanner.nextInt();
                if (numToBuy <= 0) {
                    System.out.println("不买看看也好，欢迎继续选购");
                    continue;
                }
                if (numToBuy * m.soldPrice + totalCost > customer.money) {
                    System.out.println("您带的钱不够，请重新选购");
                    continue;
                }
                if (numToBuy > m.count) {
                    System.out.println("本店库存不够，请继续选购");
                    continue;
                }
                totalCost += numToBuy * m.soldPrice;

                m.count -= numToBuy;
                littleSuperMarket.merchandiseSold[index] += numToBuy;
                customer.money -= totalCost;
                if (customer.isDrivingCar) {
                    littleSuperMarket.parkingCount++;
                }
            }
                System.out.println("顾客" + customer.name + "共消费了" + totalCost + "元钱");
                littleSuperMarket.incomingSum = totalCost;
                System.out.println("还要继续营业吗？");
                open=scanner.nextBoolean();

            }
            System.out.println("超市关门了！");
            System.out.println("今天的总营业额为："+littleSuperMarket.incomingSum+"。消费清单如下：");
            for(int i=0;i<littleSuperMarket.merchandiseSold.length;i++){
                Merchandise m=all[i];
                int numSold=littleSuperMarket.merchandiseSold[i];//每件商品的销售数量
                if(numSold>0){
                    double incoming=numSold* m.soldPrice;
                    double netIncoming=(m.soldPrice-m.purchasePrice)*numSold;
                    System.out.println(m.name+"销售了"+numSold+"件，销售额为："+incoming+"净利润为："+netIncoming);
                }
            }
        }
    }

